#define _CRT_SECURE_NO_WARNINGS 1
#include"test.h"
static node* apply(int x)
{
	node* new = (node*)malloc(sizeof(node));
	assert(new);
	new->val = x;
	new->next = NULL;
	return new;
}
void pushback(node** phead,int x)
{
	assert(phead);
	node* new= apply(x);
	if (*phead == NULL)
		*phead = new;
	else
	{
		node* cur = *phead;
		while (cur->next)
			cur = cur->next;
		cur->next = new;
	}
}
void pushhead(node** phead, int x)
{
	assert(phead);
	node* new = apply(x);
	if (*phead == NULL)
		*phead = new;
	else
	{
		new->next = *phead;
		*phead = new;
	}
}
void print(node* phead)
{
	node* cur = phead;
	while (cur)
	{
		printf("%d->", cur->val);
		cur = cur->next;
	}
}
void popback(node** phead)
{
	assert(phead);
	assert(*phead);
	node* cur = *phead;
	if (cur->next == NULL)
	{
		free(cur);
		cur = NULL;
		*phead = NULL;
	}
	else
	{
		while (cur->next->next)
			cur = cur->next;
		node* last = cur->next;
		cur->next = NULL;
		free(last);
		last = NULL;
	}
}
void pophead(node** phead)
{
	assert(phead && *phead);
	node* cur = *phead;
	*phead = cur->next;
	free(cur);
	cur = NULL;
}
node* find(node* phead, int x)
{
	if (phead == NULL)
		return NULL;
	else
	{
		node* cur = phead;
		while (cur)
		{
			if (cur->val == x)
				return cur;
			cur = cur->next;
		}
	}
}
void del(node** phead,int x)
{
	assert(*phead && x >= 0);
	node* cur = *phead;
	while (x && cur)
	{
		cur = cur->next;
		x--;
	}
	node* pre = *phead;
	while(pre->next != cur)
	{
		pre = pre->next;
	}
	pre->next = cur->next;
	free(cur);
	cur = NULL;
}